package com.hd123.rumba.jsp;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成RTree.js使用的结点数据:
 * 
 * <pre>
 *     RTreeNode root = new RTreeNode();
 *     ...
 *     dd.put(&quot;treeData&quot;, root.javascript());
 * </pre>
 * 
 * @author caili, 2006-9-8
 */
public class RTreeNode {

  /** 唯一标识 */
  private String uuid;

  /** 标题 */
  private String caption;
  /**
   * 附带的dom对象或字符串
   */
  private String object;
  private boolean stringObject = true;
  /**
   * 当需要获取此结点的子结点时, 调用的javascript function
   */
  private String onFetch;
  /**
   * 当选中此结点时, 调用的javascript function
   */
  private String onSelect;
  /**
   * 展开的图片 expanded image
   */
  private String ei;
  /**
   * 关闭的图片 collapsed image
   */
  private String ci;
  /**
   * 页结点的图片 leaf image
   */
  private String li;
  /**
   * 子结点
   */
  private List<RTreeNode> children = new ArrayList();

  /**
   * @return 形如
   * 
   * <pre>
   *     {caption:&quot;r1&quot;,object:null,onFetch:null, onSelect:null, ei:null, ci:null, li:null, children:[
   *       {caption:&quot;r11&quot;,object:null,onFetch:onFetch, onSelect:null, ei:null, ci:null, li:null, children:null},
   *       {caption:&quot;r12&quot;,object:null,onFetch:null,onSelect:onSelect, ei:null, ci:null, li:null, children:null},
   *       {caption:&quot;r13&quot;,object:null,onFetch:null, onSelect:onSelect, ei:null, ci:null, li:null, children:null}
   *      ]}
   * </pre>
   */
  public String javaScript(int indent) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < indent; ++i) {
      sb.append("  ");
    }
    String prefix = sb.toString();
    sb = new StringBuffer();
    sb.append(prefix + "{");
    sb.append("uuid:'" + getUuid() + "',");
    sb.append("caption:" + "'" + getCaption() + "',");
    if (isStringObject())
      sb.append("object:" + (getObject() == null ? "null" : "'" + getObject() + "'") + ",");
    else
      sb.append("object:" + getObject() + ",");
    sb.append("onFetch:" + getOnFetch() + ",");
    sb.append("onSelect:" + getOnSelect() + ",");
    sb.append("ei:" + (getEi() == null ? "null" : "'" + getEi() + "'") + ",");
    sb.append("ci:" + (getCi() == null ? "null" : "'" + getCi() + "'") + ",");
    sb.append("li:" + (getLi() == null ? "null" : "'" + getLi() + "'") + ",");
    sb.append("children:");
    sb.append(childrenJavaScript(indent));
    sb.append("}");
    return sb.toString();
  }

  public String childrenJavaScript(int indent) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < indent; ++i) {
      sb.append("  ");
    }
    String prefix = sb.toString();
    sb = new StringBuffer();
    sb.append("[");
    for (int i = 0; i < getChildren().size(); ++i) {
      sb.append("\n");
      sb.append(prefix);
      sb.append(getChildren().get(i).javaScript(indent + 1));
      if (i < getChildren().size() - 1) {
        sb.append(",");
      } else {
        sb.append("\n");
      }
    }
    if (getChildren().size() > 0)
      sb.append(prefix);
    sb.append("]");
    return sb.toString();
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }

  public List<RTreeNode> getChildren() {
    return children;
  }

  public void setChildren(List<RTreeNode> children) {
    this.children = children;
  }

  public String getCi() {
    return ci;
  }

  public void setCi(String ci) {
    this.ci = ci;
  }

  public String getEi() {
    return ei;
  }

  public void setEi(String ei) {
    this.ei = ei;
  }

  public String getLi() {
    return li;
  }

  public void setLi(String li) {
    this.li = li;
  }

  public String getObject() {
    return object;
  }

  public void setObject(String object) {
    this.object = object;
  }

  public String getOnFetch() {
    return onFetch;
  }

  public void setOnFetch(String onFetch) {
    this.onFetch = onFetch;
  }

  public String getOnSelect() {
    return onSelect;
  }

  public void setOnSelect(String onSelect) {
    this.onSelect = onSelect;
  }

  public boolean isStringObject() {
    return stringObject;
  }

  public void setStringObject(boolean stringObject) {
    this.stringObject = stringObject;
  }

  public static void main(String[] args) {
    RTreeNode root = new RTreeNode();
    root.setCaption("root");
    root.setObject("rootobj");
    for (int i = 0; i < 3; ++i) {
      RTreeNode c1 = new RTreeNode();
      c1.setCaption("n-" + i);
      root.getChildren().add(c1);
      for (int j = 0; j < 3; ++j) {
        RTreeNode c2 = new RTreeNode();
        c2.setObject("qq");
        c2.setStringObject(false);
        c2.setCaption("n-" + i + "-" + j);
        c1.getChildren().add(c2);
      }
    }
    System.out.print(root.javaScript(0));
  }

  public String getUuid() {
    return uuid;
  }

  public void setUuid(String uuid) {
    this.uuid = uuid;
  }
}
